package com.uuah.server.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.uuah.server.api.IUuahContext;
import com.uuah.utils.StringUtils;

public abstract class SplashMsg {
	protected List header = new ArrayList();
	protected List body = new ArrayList();
	protected List footer = new ArrayList();

	private static Logger logger = LoggerFactory.getLogger(SplashMsg.class);

	private static final Map instances = new HashMap();

	public static synchronized SplashMsg getInstance(Class clazz) {
		SplashMsg splashScreen = (SplashMsg) instances.get(clazz);

		if (splashScreen == null) {
			try {
				splashScreen = (SplashMsg) clazz.newInstance();
				instances.put(clazz, splashScreen);
			} catch (Exception ignore) {
				logger.debug(ignore.getMessage());
			}
		}

		return splashScreen;
	}

	final public void setHeader(IUuahContext context) {
		header.clear();
		doHeader(context);
	}

	final public void addBody(String line) {
		doBody(line);
	}

	final public void setFooter(IUuahContext context) {
		footer.clear();
		doFooter(context);
	}

	protected void doHeader(IUuahContext context) {
	}

	protected void doBody(String line) {
		body.add(line);
	}

	protected void doFooter(IUuahContext context) {
	}

	public String toString() {
		List boilerPlate = new ArrayList(header);
		boilerPlate.addAll(body);
		boilerPlate.addAll(footer);
		return StringUtils.getBoilerPlate(boilerPlate, '*', 70);
	}

	protected SplashMsg() {
	}
}
